package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.utils.CodeGenerateUtils;
import cn.itsource.order.domain.AdoptOrder;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.AdoptOrderMapper;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IAdoptOrderService;
import cn.itsource.org.domain.Shop;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 领养订单实现类
 */
@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PetMapper petMapper;


    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Override
    @Transactional
    public void submitOrder(Map<String, Long> param, LoginInfo loginInfo) {
        //获取用户地址id
        Long addressId =  param.get("addressId");
        //宠物id
        Long petId =  param.get("petId");
        //获取用户地址对象
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        //根据登录信息id查询user对象
        User user = userMapper.loadByLogininfoid(loginInfo.getId());
        //创建订单地址domain
        OrderAddress orderAddress = createOrderAddress(userAddress,user);
        //保存订单地址domain
        orderAddressMapper.save(orderAddress);
        //根据主键id获取宠物对象
        Pet pet = petMapper.loadById(petId);
        pet.setUser(user);
        //创建领养订单domain
        AdoptOrder adoptOrder = createAdoptOrder(pet,orderAddress);
        //保存领养订单doamin
        adoptOrderMapper.save(adoptOrder);
        //获取支付方式
        String payType = param.get("payType").toString();

        switch (payType){
            case "1":
//                跳转到银联支付界面
                break;
            case "2":
//                跳转到微信支付页面
                break;
            case "3":
                //跳转到支付宝支付页面
                break;


        }

    }

    /**
     * 创建领养订单
     * @return
     */
    private AdoptOrder createAdoptOrder(Pet pet,OrderAddress orderAddress) {
        AdoptOrder adoptOrder = new AdoptOrder();
        //摘要
        adoptOrder.setDigest("[摘要]:对"+pet.getName()+"萌宠进行领养");
        //订单金额
        adoptOrder.setPrice(pet.getSaleprice());
        //订单编号
        adoptOrder.setOrderSn(orderAddress.getOrderSn());
        //最后支付时间 当前时间+15分
//        Date lastPayTime = new Date(new Date().getTime() + 15 * 60 * 1000); 方式1
        Date lastPayTime = DateUtils.addMinutes(new Date(), 15);
        adoptOrder.setLastPayTime(lastPayTime);
        //宠物
        adoptOrder.setPet(pet);
        //用户
        adoptOrder.setUser(pet.getUser());
        //店铺
        adoptOrder.setShop(pet.getShop());
        return adoptOrder;
    }

    /**
     * 创建订单地址
     * @param userAddress
     * @return
     */
    private OrderAddress createOrderAddress(UserAddress userAddress,User user) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        //设置创建时间
        orderAddress.setCreateTime(new Date());
        //设置订单编号
        orderAddress.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        return orderAddress;
    }
}
